//index.js
const app = getApp()
const db = wx.cloud.database()
import Toast from '../../dist/toast/toast';
import Notify from '../../dist/notify/notify';
Page({
  data: {
    username: '',
    password: '',
    addusername: '',
    addpassword: '',
    usernamecontrastlength: 0,
    registerbutvalue: 0,
    loading: false,
    UserInfo: {}
  },
  onLoad: function (options) {


  },
  register: function () {
    db.collection('test').get().then(res => {
      if (res.data.filter(e => { return e.username == this.data.username }).length == 0) {
        if (!this.data.username || !this.data.password) {
          Notify({ type: 'danger', message: '请填写用户名及密码' });
        } else {
          console.log(this.data)
          db.collection('test').add({
            data: {
              username: this.data.username,
              password: this.data.password
            }
          }).then(res => {
            this.setData({
              loading: true
            })
            console.log(res)

            setTimeout(() => {
              Notify({ type: 'success', message: '注册成功' });
              this.setData({
                loading: false
              })
              this.setData({
                username: '',
                password: ''
              })
            }, 1000);
            setTimeout(() => {

              this.setData({
                registerbutvalue: 0
              })
            }, 1200);

          })
        }
      } else {
        Notify({ type: 'danger', message: '用户名重复' });
      }

    })

  },
  uservalue: function (e) {
    this.setData({
      username: e.detail
    })
    db.collection('test').get().then(res => {
      if (res.data.filter(e => { return e.username == this.data.username }).length == 0) {
        this.setData({
          usernamecontrastlength: 1
        })
      } else {
        this.setData({
          usernamecontrastlength: 2
        })
      }
    })
  },
  passwordvalue: function (e) {
    this.setData({
      password: e.detail
    })
  },
  adduservalue: function (e) {
    this.setData({
      addusername: e.detail
    })
  },
  addpasswordvalue: function (e) {
    this.setData({
      addpassword: e.detail
    })
  },
  registerbuts: function () {
    this.setData({
      registerbutvalue: 1
    })
  },
  registers: function () {
    this.setData({
      registerbutvalue: 0
    })
  },
  signin: function () {
    let that = this;
    if (!that.data.addusername || !that.data.addpassword) {
      Notify({ type: 'danger', message: '请填写用户名及密码' });
    } else {

      db.collection('test').get().then(res => {
        if (res.data.filter(e => { return e.username == that.data.addusername }).length == 0) {
          Notify({ type: 'danger', message: '用户名错误' });
        } else {

          if (res.data.filter(e => { return e.username == that.data.addusername })[0].password == that.data.addpassword) {
            db.collection('test').get().then(res => {
              wx.setStorageSync('id', res.data.filter(e => { return e.username == that.data.addusername })[0]._id)
              wx.setStorageSync('name', that.data.addusername)
              Notify({ type: 'success', message: '登录成功' });
              wx.switchTab({
                url: '../userConsole/userConsole'
              })
            })


          } else {
            Notify({ type: 'danger', message: '密码错误' });
          }
        }
      })
    }
  }
})
